<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:mesProductModel:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:mesProductModel:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:mesProductModel:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:mesProductModel:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mesProductModelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="模型名称" align="center" prop="modelName"/>
      <el-table-column label="产品" align="center" prop="name"/>
      <el-table-column label="产品型号" align="center" prop="model"/>
      <el-table-column label="产品规格" align="center" prop="specification"/>
      <el-table-column label="产品单位" align="center" prop="unit"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:mesProductModel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:mesProductModel:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品建模对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" :inline="true">
        <el-form-item label="产品" prop="name">
          <el-input disabled v-model="form.name" placeholder="请选择产品">
            <el-button v-no-more-click
                       slot="append"
                       @click="handleSelectMaterial"
                       style="background-color: #f5f7fa;
                       border-left: none;"
            >选择
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input style="width: 135%" v-model="form.modelName" placeholder="请输入模型名称"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width: 135%" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择物料档案 -->
    <MaterialSelect
      :open="materialSelectOpen"
      @onCancel="handleMaterialCancel"
      @onSelected="handleMaterialSelected"
    ></MaterialSelect>
  </div>
</template>

<script>
import {
  addMesProductModel,
  delMesProductModel,
  exportMesProductModel,
  getMesProductModel,
  listMesProductModel,
  updateMesProductModel
} from '@/api/mes/mesProductModel'
import MaterialSelect from '@/components/Material/MaterialSelect.vue'

export default {
  name: 'MesProductModel',
  components: { MaterialSelect },
  data() {
    return {
      // 选中物料信息
      material: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 产品建模数据列表
      mesProductModelList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 控制物料模态显示
      materialSelectOpen: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null
      },
      // 表单参数
      form: {
        id: null,
        name: null,
        modelName: null,
        remark: null,
        materialId: null
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '产品不能为空', trigger: 'blur' }
        ],
        modelName: [
          { required: true, message: '模型名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  watch: {
    'mesProductModelList': {
      handler(newVal, oldVal) {
        this.form.mesProductModelList = newVal
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    handleMaterialCancel() {
      this.materialSelectOpen = false
    },
    handleMaterialSelected(list) {
      if (list && list.length > 0) {
        this.material = list[0]
        this.form.materialId = this.material.id // 物料id
        this.form.name = this.material.name // 物料名
      }
      this.materialSelectOpen = false
    },
    handleSelectMaterial() {
      this.materialSelectOpen = true
    },
    /** 查询产品建模列表 */
    getList() {
      this.loading = true
      listMesProductModel(this.queryParams).then(response => {
        this.mesProductModelList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        modelName: null,
        remark: null,
        materialId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加产品建模'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMesProductModel(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改产品建模'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMesProductModel(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMesProductModel(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除产品建模编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMesProductModel(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 产品建模序号 */
    rowMesProductModelIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 产品建模添加按钮操作 */
    handleAddMesProductModel() {
      let obj = {}
      obj.modelName = ''
      obj.remark = ''
      this.mesProductModelList.push(obj)
    },
    /** 产品建模删除按钮操作 */
    handleDeleteMesProductModel() {
      if (this.checkedMesProductModel.length == 0) {
        this.$alert('请先选择要删除的产品建模数据', '提示', { confirmButtonText: '确定' })
      } else {
        this.mesProductModelList.splice(this.checkedMesProductModel[0].index - 1, 1)
      }
    },
    /** 单选框选中数据 */
    handleMesProductModelSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.mesProductModel.clearSelection()
        this.$refs.mesProductModel.toggleRowSelection(selection.pop())
      } else {
        this.checkedMesProductModel = selection
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有产品建模数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportMesProductModel(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
